/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.eclipse.jadex.adfmanager.wizards;


import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

/**
 * This page allows to get the following information in order to create the new capability
 *  - Capability name
 *    
 *  @author Pablo Muñoz
 *  @TODO verify if a capability with the same name is located in the workspace
 */

public class CapabilityWizardPage extends WizardPage {
	private Text capabilityNameText;
	private Text capabilityPackageText;

	/**
	 * Constructor for CapabilityWizardPage
	 * 
	 * @param pageName
	 */
	public CapabilityWizardPage(ISelection selection) {
		super("wizardPage");
		setTitle("Capability File");
		setDescription("This wizard creates a new capability file (with *.capability.xml extension)");
	}

	/**
	 * @see IDialogPage#createControl(Composite)
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);
		layout.numColumns = 2;
		layout.verticalSpacing = 9;
		
		Label label = new Label(container, SWT.NULL);
		label.setText("Capability name:");
		capabilityNameText = new Text(container, SWT.BORDER | SWT.SINGLE);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		capabilityNameText.setLayoutData(gd);
		capabilityNameText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		
		Label label2 = new Label(container, SWT.NULL);
		label2.setText("Package:");
		capabilityPackageText = new Text(container, SWT.BORDER | SWT.SINGLE);
		gd = new GridData(GridData.FILL_HORIZONTAL);
		capabilityPackageText.setLayoutData(gd);
		capabilityPackageText.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent e) {
				dialogChanged();
			}
		});
		initialize();
		dialogChanged();
		setControl(container);
	}

	/**
	 * Tests if the current workbench selection is a suitable container to use.
	 */
	private void initialize() {
		capabilityNameText.setText("new_capability");
	}

	/**
	 * Ensures that the all text fields are set correctly.
	 */
	private void dialogChanged() {
		if (capabilityNameText.getText().length() == 0) {
			updateStatus("Capability name must be specified");
			return;
		}
		if (capabilityNameText.getText().replace('\\', '/').indexOf('/', 1) > 0) {
			updateStatus("Capability name must be valid");
			return;
		}
		updateStatus(null);
	}

	/*
	 * This method is called in order to update the status message 
	 */
	private void updateStatus(String message) {
		setErrorMessage(message);
		setPageComplete(message == null);
	}

	/**
	 * 
	 * @return the capability name
	 */
	public String getCapabilityName(){
		return capabilityNameText.getText();
	}
	
	/**
	 * 
	 * @return the package for the new agent
	 */
	public String getCapabilityPackage(){
		return capabilityPackageText.getText();
	}
	
	/**
	 * 
	 * @return the name for the new agent file
	 */
	public String getCapabilityFileName() {
		return capabilityNameText.getText() + ".capability.xml";
	}
}